package com.yowasa.xiuxian.engine.travel.adventure;


import cn.hutool.core.util.RandomUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.travel.AdventureInterface;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.ResourceHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Set;

@Component
public class MiJingCommon implements AdventureInterface {
    @Autowired
    private ResourceHelper resourceHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Autowired
    private ItemHelper itemHelper;

    @Override
    public Set<MapEnum> limitMaps() {
        return Sets.newHashSet(MapEnum.JUE_YUN_JIAN,MapEnum.PU_GONG_YING_HAI);
    }

    @Override
    public String exec(UserBO user) {
        XiuXianExtend byAccountId = extendHelper.getByAccountId(user.getId());
        StringBuffer sb = new StringBuffer();
        double random = Math.random();
        sb.append("竟然有这样珍贵的宝物，你在秘境的探索过程中发现了一个");
        if(random < 0.5){
            //60%
            //100-200摩拉，金创，聚灵   悟道丹  无色晶体*1
            sb.append("普通的宝箱。\n打开后你收获了：\n");
            int i = RandomUtil.randomInt(100, 200);
            sb.append("原石 * 2\n");
            byAccountId.setRough(byAccountId.getRough() + 2);
            sb.append("摩拉 * ").append(i).append("\n");
            user.getUserConfig().setGold(user.getUserConfig().getGold() + i);
            ArrayList<String> comList = Lists.newArrayList("金疮药", "聚灵丹");
            ArrayList<String> speList = Lists.newArrayList("悟道丹");
            commonGet(user,comList,speList,sb);
            crystalGet(user,1,sb);

        }else if(random < 0.85){
            //30%
            //300-500摩拉  金创，聚灵，小还，月华   泣神  无色*2
            sb.append("精致的宝箱。\n打开后你收获了：\n");
            int i = RandomUtil.randomInt(300, 500);
            sb.append("原石 * 5\n");
            byAccountId.setRough(byAccountId.getRough() + 5);
            sb.append("摩拉 * ").append(i).append("\n");
            user.getUserConfig().setGold(user.getUserConfig().getGold() + i);
            ArrayList<String> comList = Lists.newArrayList("金疮药", "聚灵丹","小还丹","月华露");
            ArrayList<String> speList = Lists.newArrayList("泣神丹","小还丹","月华露");
            commonGet(user,comList,speList,sb);
            crystalGet(user,2,sb);
        }else {
            //10%
            //600-1000摩拉   小还，月华  培元  武器  无色*3
            sb.append("珍贵的宝箱。\n打开后你收获了：\n");
            int i = RandomUtil.randomInt(600, 1000);
            sb.append("原石 * 10\n");
            byAccountId.setRough(byAccountId.getRough() + 10);
            sb.append("摩拉 * ").append(i).append("\n");
            user.getUserConfig().setGold(user.getUserConfig().getGold() + i);
            ArrayList<String> comList = Lists.newArrayList("小还丹","月华露");
            ArrayList<String> speList = Lists.newArrayList("悟道丹","泣神丹","培元丹");
            commonGet(user,comList,speList,sb);
            crystalGet(user,3,sb);
            if(Math.random() > 0.9){
                String s = RandomUtil.randomEle(Lists.newArrayList("异世界行纪", "旅行剑"));
                ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(s);
                itemHelper.addEquip(user,itemInterface,1,RandomUtil.randomInt(6)+1);
            }

        }
        userHelper.lockUser(user,40,user.getName()+"正在秘境探索中");
        extendHelper.updateExtendById(byAccountId);
        return sb.toString();
    }

    private void crystalGet(UserBO user, int i, StringBuffer sb) {
        if(Math.random() > 0.88){
            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get("无色晶体");
            itemHelper.addItem(user,itemInterface,i);
            sb.append("无色晶体").append(" * ").append(i);
        }
    }

    private void commonGet(UserBO user, ArrayList<String> comList, ArrayList<String> speList, StringBuffer sb) {
        int num = RandomUtil.randomInt(2) + 1;
        String itemName;
        if(Math.random() < 0.75){
            itemName = RandomUtil.randomEle(comList);
        }else {
            itemName = RandomUtil.randomEle(speList);
        }
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(itemName);
        itemHelper.addItem(user,itemInterface,num);
        sb.append(itemName).append(" * ").append(num);

    }


    @Override
    public boolean isConflict() {
        return true;
    }

    public int weight() {
        return 25;
    }
}
